package com.spzx.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spzx.common.core.exception.ServiceException;
import com.spzx.product.api.domain.Brand;
import com.spzx.product.domain.CategoryBrand;
import com.spzx.product.mapper.CategoryBrandMapper;
import com.spzx.product.service.ICategoryBrandService;
import com.spzx.product.service.ICategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 分类品牌Service业务层处理
 */
@Service
public class CategoryBrandServiceImpl extends ServiceImpl<CategoryBrandMapper, CategoryBrand> implements ICategoryBrandService {

    @Autowired
    private CategoryBrandMapper categoryBrandMapper;

    @Autowired
    ICategoryService categoryService;

    /**
     * 查询分类品牌列表
     *
     * @param categoryCategoryBrand 分类品牌
     * @return 分类品牌
     */
    @Override
    public List<CategoryBrand> selectCategoryBrandList(CategoryBrand categoryCategoryBrand) {
        return categoryBrandMapper.selectCategoryBrandList(categoryCategoryBrand);
    }


    @Override
    public CategoryBrand selectCategoryBrandById(Long id) {
        CategoryBrand categoryBrand = this.categoryBrandMapper.selectById(id);
        if (categoryBrand == null) {
            throw new ServiceException("数据不存在");
        }
        //需要扩展临时集合属性： private List<Long> categoryIdList;
        List<Long> categoryIdList = categoryService.getAllCategoryIdList(categoryBrand.getCategoryId());
        categoryBrand.setCategoryIdList(categoryIdList);
        return categoryBrand;
    }


    @Transactional(rollbackFor = Exception.class)
    @Override
    public int insertCategoryBrand(CategoryBrand categoryBrand) {
        Long count = categoryBrandMapper.selectCount(new LambdaQueryWrapper<CategoryBrand>()
                .eq(CategoryBrand::getBrandId, categoryBrand.getBrandId())
                .eq(CategoryBrand::getCategoryId, categoryBrand.getCategoryId()));
        if (count > 0) {
            throw new ServiceException("数据已经存在");
        }
        return categoryBrandMapper.insert(categoryBrand);
    }


    @Transactional(rollbackFor = Exception.class)
    @Override
    public int updateCategoryBrand(CategoryBrand categoryBrand) {
        CategoryBrand categoryBrandDB = categoryBrandMapper.selectById(categoryBrand.getId());

        //常量池  -128 ~ 127   比较Long类型的地址还是值
        if((categoryBrandDB.getBrandId() == categoryBrand.getBrandId()) &&
                (categoryBrandDB.getCategoryId().intValue() == categoryBrand.getCategoryId().intValue())){
            return 1;
        }
        if((categoryBrandDB.getBrandId().intValue() != categoryBrand.getBrandId().intValue()) ||
                (categoryBrandDB.getCategoryId().intValue() != categoryBrand.getCategoryId().intValue())){
            Long count = categoryBrandMapper.selectCount(new LambdaQueryWrapper<CategoryBrand>()
                    .eq(CategoryBrand::getBrandId, categoryBrand.getBrandId())
                    .eq(CategoryBrand::getCategoryId, categoryBrand.getCategoryId()));
            if(count>0){ //修改的值在数据库存在，不让修改
                throw new ServiceException("数据已经存在");
            }

            return categoryBrandMapper.updateById(categoryBrand);
        }
        return 0;
    }


    @Override
    public List<Brand> selectBrandListByCategoryId(Long categoryId) {
        return categoryBrandMapper.selectBrandListByCategoryId(categoryId);
    }
}